#include "tableconstraintlist.h"

#include "tableconstraint.h"

TableConstraintList::~TableConstraintList()
{
	if(constraints != NULL) for(unsigned int i=0; i<constraints->size(); i++) delete constraints->at(i);
    delete constraints;
}

TableConstraintList::TableConstraintList(const TableConstraintList& other)
{
    if(other.constraints == NULL) this->constraints = NULL;
    else{
        constraints = new vector<TableConstraint *>();
        for(unsigned int i=0; i<other.constraints->size(); i++) {
            TableConstraint * c = other.constraints->at(i)->clone();
            constraints->push_back(c);
        }// for
    }// if-else
}

TableConstraintList::TableConstraintList(vector<TableConstraint *> * constraints)
{
    if(constraints == NULL) this->constraints = NULL;
    else{
        this->constraints = new vector<TableConstraint *>();
        for(unsigned int i=0; i<constraints->size(); i++) {
            TableConstraint * c = constraints->at(i)->clone();
            this->constraints->push_back(c);
        }// for
    }//if-else
}

int TableConstraintList::size()
{
    return constraints->size();
}

void TableConstraintList::addTableConstraint(TableConstraint * constraint)
{
    if(constraints == NULL) constraints = new vector<TableConstraint *>();
    if(constraint != NULL) constraints->push_back(constraint->clone());
}

vector<TableConstraint *> * TableConstraintList::getTableConstraints() const
{
    return constraints;
}

void TableConstraintList::setTableConstraints(vector<TableConstraint *> * constraints)
{
    //clear the list.
    for(unsigned int i=0; i<this->constraints->size(); i++) delete this->constraints->at(i);
    delete this->constraints; this->constraints = NULL;

    if(constraints != NULL){
        this->constraints = new vector<TableConstraint *>();
        for(unsigned int i=0; i<constraints->size(); i++) {
            TableConstraint * c = constraints->at(i)->clone();
            this->constraints->push_back(c);
        }// for
    }//if
}

string TableConstraintList::toString()
{
    string str = "<";
    if(constraints != NULL){
        for(unsigned int i=0; i<constraints->size(); i++){
            str = str + constraints->at(i)->toString() + ",";
        }//for
        str.erase(str.length()-1);
    }// if
    str = str + ">";
    return str;
}

TableConstraintList * TableConstraintList::clone()
{
    return new TableConstraintList(*this);
}









